package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.po.ReceiverUpdateResult;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 接收卡升级上报
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class ReceiverUpdateReport implements CompleteEntityEvent {

    /**
     * 升级进度 isFinish = true时为100
     */
    private Integer percent;

    /**
     * 升级结果,isFinish = false时为null,如果isFinish=true时结果为null，则代表所有接收卡升级失败
     */
    private List<ReceiverUpdateResult> result;

    @Override
    public ReceiverUpdateReport byteConvertObject(ByteString bytes) {
        CompleteEvent.ReceiverUpdateReport receiverUpdateReport = null;
        try {
            receiverUpdateReport = CompleteEvent.ReceiverUpdateReport.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(receiverUpdateReport);
        this.setPercent(receiverUpdateReport.getPercent());
        List<ReceiverUpdateResult> collect = receiverUpdateReport.getResultList().stream()
                .map(e -> new ReceiverUpdateResult().formProto(e)).collect(Collectors.toList());
        this.setResult(collect);
        return this;
    }
}

